<?php

	// +----------------------------------------------------------------------
	// | 邮件发送插件
	// +----------------------------------------------------------------------
	// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
	// +----------------------------------------------------------------------
	// | Author: 微尘 <yicmf@qq.com>
	// +----------------------------------------------------------------------

	namespace app\controller\ucenter;


	use app\model\ucenter\EmailHistory as EmailHistoryModel;
	use app\common\controller\Base;

	class Email extends Base
	{
		/**
		 * 邮件取消订阅.
		 */
		public function unsubscribe()
		{
			if ($this->request->isPost()) {
				$emailHistory = EmailHistoryModel::where('token', $this->request->post('token'))->find();
				if ($emailHistory) {
					$emailHistory['email']['is_subscribe'] = 0;
					$emailHistory['email']['other_reason'] = $this->request->post('other_reason');
					$emailHistory['email']['unsubscribe_id'] = $emailHistory['id'];
					$emailHistory['email']['reason'] = $this->request->post('reason/a');
					$emailHistory['email']->save();
					$this->success('已经为您取消了订阅，以后不会发送本站订阅信息！');
				} else {
					$this->error('订阅不存在');
				}
			} else {

				$reasons = [];

				for ($i = 1; $i < 6; $i++) {
					$reasons[] = [
						'id' => $i,
						'name' => lang('unsubscribe_reason_' . $i)
					];
				}
				$this->assign('token', $this->request->param('token', ''));
				$this->assign('lang', $this->request->langset());
				$this->assign('reasons', $reasons);
				return $this->fetch();
			}
		}
	}
